package com.java.code.rocketmq.batch;

import com.java.code.rocketmq.RocketmqConstants;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.util.ArrayList;
import java.util.List;

/**
 * @author weichuanyuan
 * @e-mail wei.cy@bsoft.com.cn
 * @date 2022/08/31 19:47
 * @package com.java.code.rocketmq.batch
 * @description
 */
public class BatchSend {
    public static void main(String[] args) throws Exception {
        // 实例化一个生产者来产生延时消息
        DefaultMQProducer producer = new DefaultMQProducer(RocketmqConstants.GROUP);
        // 设置NameServer的地址
        producer.setNamesrvAddr(RocketmqConstants.NAMESVR_ADDR);
        // 启动生产者
        producer.start();
        int totalMessagesToSend = 100;
        //每次只发送不超过4MB的消息,都可以使用批处理
        List<Message> messages = new ArrayList<>();
        for (int i = 0; i < totalMessagesToSend; i++) {
            Message message = new Message(RocketmqConstants.TOPIC, ("Hello scheduled message " + i).getBytes());
            messages.add(message);
        }
        producer.send(messages);
        // 关闭生产者
        producer.shutdown();
    }
}
